package com.bupt.liutong.admin.bbs.bo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.bupt.liutong.admin.bbs.dao.BoardDao;
import com.bupt.liutong.admin.bbs.dto.Board;
import com.bupt.liutong.admin.bbs.form.BoardForm;
import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.util.BeanUtils;

public class BoardBo extends BaseBo {

	private BoardDao boardDao;

	public List<BoardForm> getBoardsByPage(BoardForm boardForm,
			HttpServletRequest request) {
		Board board = new Board();
		sm.pagingWithNoCriteria(boardForm, board, "bbs_board.getBoardsCount");
		List<Board> boards = boardDao.getBoardsByPage(board);
		return BeanUtils.dtoToForm(boards, BoardForm.class);
	}

	public List<BoardForm> getBoards(BoardForm boardForm) {
		Board board = new Board();
		List<Board> boards = boardDao.getBoards(board);
		return BeanUtils.dtoToForm(boards, BoardForm.class);
	}

	public BoardForm getBoardById(BoardForm boardForm) {
		BoardForm resultForm = new BoardForm();
		Board board = boardDao.getBoardById(boardForm.getId());
		BeanUtils.copyProperties(resultForm, board);
		return resultForm;
	}
	
	public List<BoardForm> getAllBoards(){
		return BeanUtils.dtoToForm(boardDao.getAllBoards(), BoardForm.class);
	}

	@Transactional
	public void insertBoard(BoardForm boardForm) {
		Board board = new Board();
		BeanUtils.copyProperties(board, boardForm);
		boardDao.insertBoard(board);
	}

	@Transactional
	public void updateBoard(BoardForm boardForm) {
		Board board = new Board();
		BeanUtils.copyProperties(board, boardForm);
		boardDao.updateBoard(board);
	}

	@Transactional
	public void deleteBoards(String[] ids) {
		if (ids != null) {
			for (int i = 0; i < ids.length; i++) {
				boardDao.deleteBoard(Integer.parseInt(ids[i]));
			}
		}
	}

	public void setBoardDao(BoardDao boardDao) {
		this.boardDao = boardDao;
	}
}
